/*
 *  ekk_decoder_subtitle_file.c
 *  EKK_Player_Lib
 *
 *  Created by hsh on 11-8-22.
 *  Copyright 2011xunlei. All rights reserved.
 *
 */

//this lib need md5 and sha1 get the cid and svvcid
#include "ekk_subtitle/ekk_decoder_subtitle_file.h"

#if EKK_ENABLE_SUBTITLE

#include "ekk_subtitle/ekk_subtitle_dec_pic.h"
//#include "ekk_interface.h"
//#include "ekk_decoder/ffmpeg_decoder_proxy_implement.h"

#if EKK_ENABLE_NET_SUBTITLE
#define  DOWNLOADOPEN 1
#else
#define  DOWNLOADOPEN 0
#endif /*#if EKK_ENABLE_NET_SUBTITLE*/

#if DOWNLOADOPEN
#define LINUX
#include "common/define.h"
#include "common.h"

#include "common/md5.h"
#include "common/utility.h"
#include "et_task_manager.h"
#include "ekk_socket.h"
#else
#include "ekk_subtitle/sd_iconv.h"
#endif

#include <dirent.h>
#include <sys/stat.h>

#if DOWNLOADOPEN
int http_request = 0;
int http_post_sucs = 0;
char s_http_recv[1024] = {0};

#endif
#include <iconv.h>
#define MAX_SUBTITLE_LINE_NUM 2048
#define UTF8BLOCKS 160

#if EKK_ENABLE_NET_SUBTITLE
#define SUBTITLESEVERURL "zimu.gougou.com"
#endif /*#if EKK_ENABLE_NET_SUBTITLE*/

#define SUBTITLESEVERMATCHPORT  18889
#define SUBTITLESEVERUPLOADPORT 18888

#define WAIT_TIME_MICROSEC (5000)


static char* g_match_type_str[SUBTITLE_MATCH_TYPE_NUM] = {"SRT","ASS","SSA","SMI","PSB","srt","ass","ssa","smi","psb"};


#define SUBTITLE_MATCH_TYPE_NET_NUM 10
static char* g_match_net_type_str[SUBTITLE_MATCH_TYPE_NET_NUM] = {"中英双语","未知／其他","简体中文","繁体中文","英语","韩语","日文",0,0,0};


//subtitle struct
typedef struct tagTAV_SubTitle{
	int index;
	long long starttime;
	long long endtime;
	char* ptext;
}TAV_SubTitle;

#define MAX_TAV_SUBTITLE_NUM 7200
#define MAX_LINES_IN_A_UNIT 2


static ekk_bool g_is_requesting_http = ekk_false;
static ekk_bool g_is_requesting_url = ekk_false;
static ekk_bool g_url_has_been_canceled = ekk_false;
static ekk_bool g_http_has_been_canceled = ekk_false;

#if EKK_ENABLE_NET_SUBTITLE
#define NONE_HTTP_ID (-1)
static _u32 g_http_id = NONE_HTTP_ID;
#endif

//static process_subtitle_start_result_func_ptr g_process_subtitle_start_result_func_ptr = 0;

static SUBTITLE_MATCH_CONTEXT g_match_ctx ={0};

typedef struct subtitle_context_tag{
	void* p_sub; //subtitle txt to bmp context
	TAV_SubTitle tav_subtitle[MAX_TAV_SUBTITLE_NUM];
	int tav_subtitle_count;
	int cur_subtitle_index;
	int cur_subtitle_get_num;
	int cur_no_content_num;
}SUBTITLE_CONTEXT;	
///*
#if __DARWIN_64_BIT_INO_T
struct xdirent {
	__uint64_t  d_ino; 
	__uint64_t  d_seekoff; 
	__uint16_t  d_reclen;
	__uint16_t  d_namlen; 
	__uint8_t   d_type;	
	char d_name[1024];	
};
#endif	


//*/
//void set_subtitle_start_result_func(process_subtitle_start_result_func_ptr func)
//{
//	g_process_subtitle_start_result_func_ptr = func;
//}

#if EKK_ENABLE_NET_SUBTITLE
void ekk_subtitle_download_file(char* url ,char* path, char* filename);
#endif

static void sort_subtitle_array(SUBTITLE_CONTEXT* p);

int ekk_find_file_exist(char* path)
{
	int ret = -1;
	EKK_FILE_HANDLE fd = ekk_fopen(path, "rb");
	if (fd) {
		ret = 0;
		ekk_fclose(fd);
	}
	return ret;
}	

void ekk_subtitle_cancel_load_by_path()
{
    if (g_is_requesting_http)
    {
#if EKK_ENABLE_NET_SUBTITLE
        if (g_http_id != NONE_HTTP_ID)
        {
            etm_http_cancel(g_http_id);

        }
#endif
    }
    g_http_has_been_canceled = ekk_true;
    g_is_requesting_http = ekk_false;
}

//net = 0 is local file path is full path ,net = 1,path is the net math back subtitle
SUBTITLE_HANDLE ekk_subtitle_load_by_path(int net, char* path, void *xmp_player, process_subtitle_start_result_func_ptr func, SUBTITLE_HANDLE *subtitle_handle)
{
    SUBTITLE_CONTEXT* p;
    g_http_has_been_canceled = ekk_false;
	p = ekk_malloc(sizeof(SUBTITLE_CONTEXT));
    
	ekk_memset(p, 0,sizeof(SUBTITLE_CONTEXT));
	if (p) {
		p->p_sub = dec_subtitle_creat();
		//p->cur_subtitle_index = 0;
		//p->tav_subtitle_count = 0;
		//p->cur_subtitle_index = 0;
		//p->cur_subtitle_get_num = 0;		
	}
	//p_sub = dec_subtitle_creat();
	int ret = -1;
	//local file

	*subtitle_handle = (SUBTITLE_HANDLE)p;
	
	if (net == 0) {
		ret = ekk_subtitle_load_from_out_file((SUBTITLE_HANDLE)p,path,NULL);
		func(xmp_player, ret, path);
	}
	//net file
	else if(net == 1)
	{
		//math the net file
		int i = 0;
		
		for(i = 0; i < g_match_ctx.subtitle_net_url_count;i++)
		{	
			if (ekk_strcmp(g_match_ctx.subtitle_net_url[i].filename,path) == 0) {
				
					char subpath[512]={0};
				//find the local cid file
				/*
				char subpath[512]={0};
				ekk_sprintf(subpath,"%s%s",g_match_ctx.pathdir,g_match_ctx.subtitle_net_url[i].cid);//,g_subtitle_net_url[0].cid);
				if (ekk_find_file_exist(subpath) == 0) {
					
				}
				else {
				//*/ 
#if EKK_ENABLE_NET_SUBTITLE
					ekk_memset(subpath, 0, 512);					
					ekk_sprintf(subpath,"%s",g_match_ctx.pathdir);//,g_subtitle_net_url[0].cid);
					char savename[512] = {0};
					ekk_sprintf(savename,"%s%s",g_match_ctx.sfilename,g_match_ctx.subtitle_net_url[i].format);
					ekk_subtitle_download_file(g_match_ctx.subtitle_net_url[i].url,subpath,savename);
                    
                    while (g_is_requesting_http)
                    {
                        ekk_sleep(WAIT_TIME_MICROSEC);
                    }

					if (g_http_has_been_canceled)
                    {
                        ekk_free(p);
                        return NULL;
                    }
					ekk_memset(subpath, 0, 512);
					
					ekk_sprintf(subpath,"%s%s",g_match_ctx.pathdir,savename);
#endif
					
				//}
				
				//no download the cid file 
				
				//decode the downloaded file
				ret = ekk_subtitle_load_from_out_file((SUBTITLE_HANDLE)p,subpath,g_match_ctx.subtitle_net_url[i].format);
				func(xmp_player, ret, subpath);

				break;
			}
			
		}
	}	
	return (SUBTITLE_HANDLE)p;
}

void ekk_subtitle_cancel_find_match_file()
{
    g_url_has_been_canceled = ekk_true;
#if EKK_ENABLE_NET_SUBTITLE
    ekk_cancel_sock_deal(SUBTITLESEVERURL, SUBTITLESEVERMATCHPORT);
#endif
}

//match file by moive full path 
int ekk_subtitle_find_match_file(char* path, void *param, process_subtitle_get_match_path_func_ptr func)
{
	//according path get filename
	int ret = -1;
    g_url_has_been_canceled = ekk_false;
	//init
	ekk_memset(&g_match_ctx,0,sizeof(SUBTITLE_MATCH_CONTEXT));
	if (!path) {
		return ret;
	}	
	char* psub = path;
	char* filename = psub;
	while (psub != NULL) {
		psub++;
		filename = psub;
		psub = ekk_strchr(psub, '/');
	}
	int pathlen = ekk_strlen(path);
	ekk_memcpy(g_match_ctx.path,path,pathlen);
	g_match_ctx.path[pathlen+1] = 0;
	//local match
	g_match_ctx.local_match_filepath_count = 0;
	g_match_ctx.subtitle_net_url_count = 0;
	if (filename) {
		int len = filename - path;
		char fpath[EKK_MAX_URL_LEN] ={0};
		ekk_memcpy(fpath, path, len);
		ekk_memcpy(g_match_ctx.pathdir, path,len);
		int tlen = ekk_strlen(path);
		int plen = ekk_strlen(fpath);
		int flen = ekk_strlen(filename);
		if (tlen == plen + flen) {
			//filename deal 
			char* filenamp = filename;
			char* filenamb = filenamp;
			int k = 0;
			while (filenamp) {
				filenamp++;
				filenamb = filenamp;
				k++;
				filenamp = ekk_strchr(filenamp, '.');
				
			}
			if (filenamb && k>1) {
				int fblen = filenamb - filename;
				char filenames[EKK_MAX_URL_LEN] ={0};
				if (fblen > 1) {
					ekk_memcpy(filenames, filename, fblen);
					ekk_memcpy(g_match_ctx.sfilename, filename, fblen);
					//have ok filename 
					int m = 0;
					//new match rules
					struct dirent **namelist;
					int total;
					total = scandir(fpath,&namelist, 0, 0);
#if __DARWIN_64_BIT_INO_T
					for (m =0; m<total; m++) {
						struct xdirent *pmt = NULL;
						pmt = (struct xdiren*)namelist[m];
						printf("\n[Cur Dir File Name %d] = %s \n", m,pmt->d_name);
						char* pstrsub = ekk_strstr(pmt->d_name,filenames);
						if (pstrsub) {
							pstrsub +=fblen;
							int mi = 0;
							for (mi =0; mi<SUBTITLE_MATCH_TYPE_NUM; mi++) {
								char* pstrtype = ekk_strstr(pstrsub,g_match_type_str[mi]);
								if (pstrtype) {
									char ppath[EKK_MAX_URL_LEN]={0};
									sprintf(ppath,"%s%s",fpath,pmt->d_name);
									int ppathlen = ekk_strlen(ppath);															
									ekk_memcpy(g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count], ppath,ppathlen);
									g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count][ppathlen+1] = 0;
									g_match_ctx.local_match_filepath_count++;										
								}
								
							}
						}
					}				
#else					
					for (m =0; m<total; m++) {

						char* pstrsub = ekk_strstr(namelist[m]->d_name,filenames);
						if (pstrsub) {
							pstrsub +=fblen;
							int mi = 0;
							for (mi =0; mi<SUBTITLE_MATCH_TYPE_NUM; mi++) {
								char* pstrtype = ekk_strstr(pstrsub,g_match_type_str[mi]);
								if (pstrtype) {
									char ppath[EKK_MAX_URL_LEN]={0};
									sprintf(ppath,"%s%s",fpath,namelist[m]->d_name);
									int ppathlen = ekk_strlen(ppath);															
									ekk_memcpy(g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count], ppath,ppathlen);
									g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count][ppathlen+1] = 0;
									g_match_ctx.local_match_filepath_count++;										
								}
								
							}
						}
					}					
#endif					

					
					
					/*
					//struct stat sb;
					DIR *d;
					struct dirent *file;//readdir function back data					
					
					if ((d = opendir(fpath))) {
						while ((file = readdir(d)) != NULL) {
							if (strncmp(file->d_name,".",1) == 0) {
								continue;
							}

						}
						
					}
					else {
						//printf("ekk_subtitle match error path \n")
					}
					//*/ 
					
					// old match rules
					/*
					for (m =0; m<SUBTITLE_MATCH_TYPE_NUM; m++) {
						char ppath[EKK_MAX_URL_LEN]={0};
						sprintf(ppath,"%s%s%s",fpath,filenames,g_match_type_str[m]);
						int a = ekk_find_file_exist(ppath);	
						if (a == 0) {
							//the type subtitle exist
							int ppathlen = ekk_strlen(ppath);
							//g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count] = ekk_malloc(ppathlen +1);							
							ekk_memcpy(g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count], ppath,ppathlen);
							g_match_ctx.local_match_filepath[g_match_ctx.local_match_filepath_count][ppathlen+1] = 0;
							g_match_ctx.local_match_filepath_count++;
							
							
						}
					}
					//*/ 

				while(total--)
					free(namelist[total]);
				free(namelist);
				}
				
			}
			
		}
		g_match_ctx.param = param;
		func(&g_match_ctx);
#if EKK_ENABLE_NET_SUBTITLE
		//net match the subtitle  send net 
		ekk_get_subtitle_url_from_net(path,NULL);
		g_match_ctx.param = param;
        if (!g_url_has_been_canceled)
        {
            func(&g_match_ctx);
        }
#endif
		
		ret = 0;
	}
	return ret;
}	

//make a subtitle pic address safe malloc or other init 
int ekk_subtitle_init()
{
	//char path[1024]={0};
	//ekk_sprintf(path, "%s%s", ekk_getcwd(), "/Documents/2.srt");
	//ekk_subtitle_find_match_file(path);
	int ret = 0;	

	int wd = 0;
	int hd = 0;
	unsigned long rgba = 0;
	int fontsize =0;
	char* fonttype = NULL;

	dec_subtitle_init();

	return ret;
}
//uninit the subtitle decoder
int ekk_subtitle_uninit()
{
	
	int ret = 0;

	return ret;
}
//close the subtitle and free malloc byte
void ekk_subtitle_close(SUBTITLE_HANDLE hd)
{
	int i = 0;
	SUBTITLE_CONTEXT* p =(SUBTITLE_CONTEXT*)hd;
	
	if (p) 
    {
		dec_subtitle_close(p->p_sub);
        ekk_free(p);
        p = 0;
	}

	//dec_subtitle_uninit();
	/*
	//cotent free
	if (g_tav_subtitle_count > 0) {
		
		for (i= 0; i<g_tav_subtitle_count; i++) {
			if(g_tav_subtitle[i].ptext)
			{
				ekk_free(g_tav_subtitle[i].ptext);
				g_tav_subtitle[i].ptext = NULL;
			}	
		}
	}
	//auto match free
	if(g_local_match_filepath_count >0)
	{
		for (i = 0; i<g_local_match_filepath_count; i++) {
			if(g_local_match_filepath[i])
			{
				ekk_free(g_local_match_filepath[i]);
				g_local_match_filepath[i] = 0;
			}	
		}
	}
	dec_subtitle_init();
	//*/ 
}	
//set subtitle text attribute
void ekk_subtitle_set_text_attribute(SUBTITLE_HANDLE hd,SUBTITLE_TEXT_ATTRIBUTE* ptext)
{
	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if(p)
	{	
		dec_set_attribute_subtitle(&ptext->x, &ptext->y, &ptext->rgba, ptext->fonttype, &ptext->fontsize,p->p_sub);

	}
}	
//format 0:00:00 to time long long
static long long ekk_inline srt_time_str_sec_to_time(char* timestr)
{
	long long ret = 0;
	char ttstr[9]={0};
	char* linetxt = timestr;
	int len = ekk_strlen(timestr);
	int m = 0;
	int nhour = 0,nmin =0,nsec =0,nmicrosec =0;
	int mx,my;
	mx = 0;
	my = 0;
	int nindex = 0;
	while (m<len) {					
		if (*(linetxt+m) == ':' || *(linetxt+m) == ',' || *(linetxt+m) == '.') {
			if (my > 0)
				mx = my+1;
			else 					
				mx = my;
			my = m;
			ekk_memcpy(ttstr,linetxt+mx,my-mx);
			ttstr[my-mx] = 0;
			switch (nindex) {
				case 0:
					nhour = atoi(ttstr);
					break;
				case 1:
					nmin = atoi(ttstr);
					break;
				case 2:
					nsec = atoi(ttstr);
					break;				
				default:
					break;
			}
			nindex++;
			
		}
		m++;
	}
	// last sec
	if(my <len && my >0)
	{
		ekk_memcpy(ttstr,linetxt+my+1,len-my-1);
		ttstr[len-my-1] = 0;
		int ttstrlen = ekk_strlen(ttstr);
		nsec = atoi(ttstr);
		
	}	
	
	ret = (nhour*3600 + nmin*60 + nsec) *1000; 
	return ret;
	
}

//format 0:00:00.000  0:00:00,000 to time long long 
static long long ekk_inline srt_time_str_to_time(char* timestr)
{
	long long ret = 0;
	char ttstr[9]={0};
	char* linetxt = timestr;
	int len = ekk_strlen(timestr);
	int m = 0;
	int nhour = 0,nmin =0,nsec =0,nmicrosec =0;
	int mx,my;
	mx = 0;
	my = 0;
	int nindex = 0;
	while (m<len) {					
		if (*(linetxt+m) == ':' || *(linetxt+m) == ',' || *(linetxt+m) == '.') {
			if (my > 0)
				mx = my+1;
			else 					
				mx = my;
			my = m;
			ekk_memcpy(ttstr,linetxt+mx,my-mx);
			ttstr[my-mx] = 0;
			switch (nindex) {
				case 0:
					nhour = atoi(ttstr);
					break;
				case 1:
					nmin = atoi(ttstr);
					break;
				case 2:
					nsec = atoi(ttstr);
					break;				
				default:
					break;
			}
			nindex++;
			
		}
		m++;
	}
	// last microsec
	if(my <len && my >0)
	{
		ekk_memcpy(ttstr,linetxt+my+1,len-my-1);
		ttstr[len-my-1] = 0;
		int ttstrlen = ekk_strlen(ttstr);
		nmicrosec = atoi(ttstr);
		if (ttstr[0] != '0' && nmicrosec < 100) {
			if (nmicrosec < 10) {
				nmicrosec *= 100;
			}
			else {
				nmicrosec *=10;
			}
			
		}
	}	
	
	ret = (nhour*3600 + nmin*60 + nsec) *1000 + nmicrosec; 
	return ret;
}

static int subtitle_split_multilines(SUBTITLE_HANDLE hd)
{
    int ret = -1;
    SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) 
    {
		return ret;
	}
    
    int j = 0, k = 0, m = 0, tlu = 0;
    TAV_SubTitle* s_array = ekk_malloc(sizeof(TAV_SubTitle)*MAX_TAV_SUBTITLE_NUM);
    ekk_memset(s_array, 0, sizeof(TAV_SubTitle)*MAX_TAV_SUBTITLE_NUM);
    char* p_end = NULL;
    char* p_raw = NULL;
    char* p_t_end = NULL;
    long long d_ts = 0;
    for (int i = 0; i < p->tav_subtitle_count; ++i)
    {
        k = 0;
        p_raw = p->tav_subtitle[i].ptext;

		//avoid null character
		//modify.by.wangshneg
		if (p_raw == 0)
		{
			continue;
		}
		
        while (p_end = ekk_strchr(p_raw, '\n'))
        {
            //just for devide into lines unit
            p_t_end = p_end + 1;
            tlu = MAX_LINES_IN_A_UNIT - 1;
            while (tlu > 0 && (p_t_end = ekk_strchr(p_t_end, '\n')))
            {
                p_end = p_t_end;
                ++p_t_end;
                --tlu;
            }
            //end
            s_array[j].index = j + 1;
            p_end[0] = 0;
            s_array[j].ptext = ekk_malloc(ekk_strlen(p_raw)+1);
            ekk_strcpy(s_array[j].ptext, p_raw);
            p_raw = &p_end[1];
            ++j;
            ++k;
        }
        if (p_raw[0])
        {
            s_array[j].index = j + 1;
            s_array[j].ptext = ekk_malloc(ekk_strlen(p_raw)+1);
            ekk_strcpy(s_array[j].ptext, p_raw);
            ++j;
            ++k;
        }
        d_ts = (p->tav_subtitle[i].endtime - p->tav_subtitle[i].starttime)/k;
        for (m = 0; m < k; ++m)
        {
            s_array[j - k + m].starttime = p->tav_subtitle[i].starttime + d_ts * m;
            s_array[j - k + m].endtime = s_array[j - k + m].starttime + d_ts;
        }
        ekk_free(p->tav_subtitle[i].ptext);
    }
    ekk_memcpy(p->tav_subtitle, s_array, sizeof(TAV_SubTitle)*MAX_TAV_SUBTITLE_NUM);
    p->tav_subtitle_count = j;
    ekk_free(s_array);
    return 0;
}

#define MAX_CHAR_FILTER		2
char g_chFilter[MAX_CHAR_FILTER][30] = 
{
	"{\\an",
	"{\\pos(",
};
	
//decode srt file
int load_subtitle_srt_file(char* path,SUBTITLE_HANDLE hd)
{
	int ret = -1;
	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) 
	{
		return ret;
	}
	
	EKK_FILE_HANDLE mfd = ekk_fopen(path, "rb");
	if (mfd) 
	{
		int index = 0;
		int nindex = 0;
        int line_idx = 0;
		int bValidIndex = 0;
		int nSubCount = 0;	//ÓÐÐ§×ÖÄ»Êý
		char* linetxt = ekk_malloc(MAX_SUBTITLE_LINE_NUM+1);
		fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
		while (ekk_strlen(linetxt) > 0) 
		{
			if (isdigit(linetxt[0]))
			{
				bValidIndex = 1;
				nindex = atoi(linetxt);
				index = nindex;
			}
			
			if (bValidIndex = 1)			
			{
				p->tav_subtitle[index-1].index = index;
				//find a subtitle block
				//get time line 		
				
				memset(linetxt,0,MAX_SUBTITLE_LINE_NUM +1);
				fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
				//analyze time 
				int m = 0;
				char* psub = ekk_strchr(linetxt,'>');
				if (psub) 
				{
					p->tav_subtitle[index-1].endtime = srt_time_str_to_time(psub +1);
					while (linetxt[m] != '-' ) 
					{					
						m++;
					}
					linetxt[m] =  0;
					p->tav_subtitle[index-1].starttime = srt_time_str_to_time(linetxt);
				}
				
				
				//find txt line 1
				memset(linetxt,0,MAX_SUBTITLE_LINE_NUM + 1);
				fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
				int txtlen = ekk_strlen(linetxt);
				int ttlen = txtlen;
				int toldlen = txtlen;
				p->tav_subtitle[index-1].ptext = ekk_malloc(ttlen+1);
				ekk_memcpy(p->tav_subtitle[index-1].ptext, linetxt, txtlen);				
				nindex = atoi(linetxt);
				while (nindex != index +1 && txtlen > 0) 
				{
					//multiply line txt
					memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
					fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
					
					txtlen = ekk_strlen(linetxt);
					
					if (txtlen > 2 ) 
					{
						ttlen +=txtlen;
						nindex = atoi(linetxt);							
						p->tav_subtitle[index-1].ptext = ekk_realloc(p->tav_subtitle[index-1].ptext,ttlen+1);
						ekk_memcpy(p->tav_subtitle[index-1].ptext + toldlen, linetxt, txtlen);
                        p->tav_subtitle[index-1].ptext[toldlen + txtlen] = 0;
						toldlen += txtlen;
					}
					else 
					{
						memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
						fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
						break;
					}

					/*
					ttlen +=txtlen;
					nindex = atoi(linetxt);
					if (nindex == index +1) 
					{
						//toldlen = txtlen;
						break;
					}
					
					if (txtlen > 2 ) 
					{
						g_tav_subtitle[index-1].ptext = ekk_realloc(g_tav_subtitle[index-1].ptext,ttlen+1);
						ekk_memcpy(g_tav_subtitle[index-1].ptext + toldlen, linetxt, txtlen);
						toldlen += txtlen;
					}
					//*/ 
				}
				
				*(p->tav_subtitle[index-1].ptext+ttlen) = 0;

				//modify.by.wangsheng
				//remove{\an2}

				for (int nCnt = 0; nCnt < MAX_CHAR_FILTER; nCnt++)
				{
					char* pPrexSub = ekk_strstr(p->tav_subtitle[index-1].ptext, g_chFilter[nCnt]);
					if (pPrexSub) 
					{
						char* pSufxSub = ekk_strchr(pPrexSub,'}');
						if (pSufxSub) 
						{
							int nFilterLens = pSufxSub - pPrexSub + 1;
							int nMoveLens = ttlen - (pSufxSub - p->tav_subtitle[index-1].ptext) - 1;
							//ÒÆ¶¯×Ö·û´®
							memmove(pPrexSub,pSufxSub+1,nMoveLens);
							*(p->tav_subtitle[index-1].ptext+ttlen-nFilterLens) = 0; 
						}
					}
				}
				
				nSubCount++;

				continue;
			}
			
			memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
			fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
			bValidIndex = 0;
		}
		
		p->cur_subtitle_index = 0;
		p->tav_subtitle_count = nSubCount;
		if (nSubCount > 0) 
		{
			ret = 0;
		}		
		
		ekk_free(linetxt);
		ekk_fclose(mfd);
        sort_subtitle_array(p);
	}
    
	return ret;
}


static void sub_loc_qsort(SUBTITLE_CONTEXT* p, int l, int r)
{
    int i, j;
    long long x;
    TAV_SubTitle t = {0};
    if (l < r)
    {
        i = l;
        j = r;
        t = p->tav_subtitle[i];
        x = p->tav_subtitle[i].starttime;
        while (i < j)
        {
            while(i < j && p->tav_subtitle[j].starttime > x) j--; /* 从右向左找第一个小于x的数 */
            if(i < j)
            {
                p->tav_subtitle[i++] = p->tav_subtitle[j];
            }
            while(i < j && p->tav_subtitle[i].starttime < x) i++; /* 从左向右找第一个大于x的数 */
            if(i < j)
            {
                p->tav_subtitle[j--] = p->tav_subtitle[i];
            }
        }
        p->tav_subtitle[i] = t;
        sub_loc_qsort(p, l, i-1); /* 递归调用 */
        sub_loc_qsort(p, i+1, r);
    }
}

static void sort_subtitle_array(SUBTITLE_CONTEXT* p)
{
    sub_loc_qsort(p, 0, p->tav_subtitle_count - 1);
}


//decode ssa or ass file
int load_subtitle_ssa_file(char* path,SUBTITLE_HANDLE hd)
{
	int ret = -1;
	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) {
		return ret;
	}	
	//ass[v4 + styles]  similar ssa [v4 styles]
	//Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text	
	EKK_FILE_HANDLE mfd = ekk_fopen(path, "rb");
	if (mfd) {
		int index = 0;
		int nindex = 0;
		char* linetxt = ekk_malloc(MAX_SUBTITLE_LINE_NUM+1);
		fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);	
		char tmpbuf[2048]={0};
		char tscript[9] = {0};
		//char timestr[20] = {0};
		char starttime[20] ={0};
		char endtime[20]= {0};
        char* tpsub = 0;
		
		while (ekk_strlen(linetxt) > 0) {
			
			int nSubImage = 0; //check ssa is image subtitle
			int len = 0;
			char* psub = 0;
			char* psubtitle = 0;
			int maxtag = 9;//the find the Text tag
			int ntag = 0;
			memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
			fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);	
			len = ekk_strlen(linetxt);
			ekk_strcpy(tmpbuf,linetxt);
			if (len > 9) {
				ekk_memcpy(tscript,tmpbuf,8);
				if (ekk_strcmp(tscript, "Dialogue") != 0) 
					continue;
			}
			else {
				continue;
			}
			
			
			psub = ekk_strchr(tmpbuf,',');
			while(psub)
			{
				ntag++;
				if (ntag >= maxtag) {
					psub += 1;
					psubtitle = psub;
					break;
				}
				psub += 1;
				if (ntag == 1) {
					int m = 0;
					while (*(psub +m) != ',') {
						starttime[m] = *(psub+m);
						m++;
					}
					starttime[m] = 0;
					
				}
				else if (ntag == 2) {
					int m = 0;
					while (*(psub +m) != ',') {
						endtime[m] = *(psub+m);
						m++;
					}
					endtime[m] = 0;
					
				}			
				else if (ntag == 3) {
					ekk_memset(tscript,0,9);
					ekk_memcpy(tscript, psub, 5);
					if(ekk_strcmp(tscript,"image") ==0 )
						nSubImage = 1;
				}
				psubtitle = psub;
				//p->subtitle_size = ekk_strlen(psub);
				//ekk_memcpy(p->subtitle_buffer, psub, p->subtitle_size);
				psub = ekk_strchr(psub,',');
			}
			//image no deal
			if (nSubImage == 1) {
				continue;
			}
			index++;
			p->tav_subtitle[index-1].index = index;
			p->tav_subtitle[index-1].starttime = srt_time_str_to_time(starttime);
			p->tav_subtitle[index-1].endtime = srt_time_str_to_time(endtime);
            
            {
                char* dst_str = NULL;
                int ttlen = 0;
                int state = 0;//0 = 文字， 1 = 代码
                int it = 0;
                int idst = 0;
                char* ret_sym = NULL;//换行号
                
                if (psubtitle)
                {
                    ttlen  = ekk_strlen(psubtitle);
                    p->tav_subtitle[index-1].ptext = ekk_malloc(ttlen+1);
                    dst_str = p->tav_subtitle[index-1].ptext;
                    ekk_memset(dst_str, 0, ttlen + 1);
                    
                    state = 0;
                    for (it = 0; it < ttlen; ++it)
                    {
                        char tc = psubtitle[it];
                        if (tc == '{')
                        {
                            state = 1;
                        }
                        if (state == 0)
                        {
                            dst_str[idst] = tc;
                            idst++;
                        }
                        if (tc == '}')
                        {
                            state = 0;
                        }
                    }
                    
                    /*
                    ret_sym = dst_str;//去掉原有换行号
                    while (ret_sym = ekk_strstr(ret_sym, "\r\n"))
                    {
                        ret_sym[0] = ' ';
                        ret_sym[1] = ' ';
                        ret_sym = &ret_sym[2];
                    }*/
                    
                    ret_sym = dst_str;
                    while (ret_sym = ekk_strstr(ret_sym, "\\N"))
                    {
                        ret_sym[0] = '\r';
                        ret_sym[1] = '\n';
                        ret_sym = &ret_sym[2];
                    }
                }
            }
            
		}
		ekk_free(linetxt);
		linetxt = 0;
		p->cur_subtitle_index = 0;
		p->tav_subtitle_count = index;
		if (index > 0) {
			ret = 0;
		}		
		ekk_fclose(mfd);
        sort_subtitle_array(p);
	}
	return ret;
}
//decode sub file
int load_subtitle_sub_file(char* path,SUBTITLE_HANDLE hd)
{
	EKK_FILE_HANDLE mfd = ekk_fopen(path, "rb");
	if (mfd) {
		
		ekk_fclose(mfd);
	}
}
//decode psb file
int load_subtitle_psb_file(char* path,SUBTITLE_HANDLE hd)
{
	int ret = -1;
	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) {
		return ret;
	}	
	EKK_FILE_HANDLE mfd = ekk_fopen(path, "rb");
	if (mfd) {
		char* linetxt = ekk_malloc(MAX_SUBTITLE_LINE_NUM+1);
		fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);	
		char tmpbuf[2048]={0};
		char starttime[20] ={0};
		char endtime[20]= {0};
		char* psub = 0;
		int index = 0;
		int j =0;
		int len = ekk_strlen(linetxt);
		while (len > 0) {
			ekk_strcpy(tmpbuf, linetxt);
			psub = ekk_strchr(tmpbuf,'{');
			if (psub) {
				//have psb line
				//start time
				j = 0;
				psub = psub+1;
				ekk_memset(starttime, 0, 20);
				while (*(psub+j) != '}' && j<20) {
					*(starttime+j) = *(psub+j);
					j++;
				}
				
				//end time
				psub = ekk_strchr(psub,'{');
				if (psub) {
					j = 0;
					psub++;
					ekk_memset(endtime, 0, 20);
					while (*(psub+j) != '}' && j<20) {
						*(endtime+j) = *(psub+j);
						j++;
					}
					psub = ekk_strchr(psub, '}');
					if (psub) {
						psub++;
						//get info
						index++;
						p->tav_subtitle[index-1].index = index;
						p->tav_subtitle[index-1].starttime = srt_time_str_sec_to_time(starttime);
						p->tav_subtitle[index-1].endtime = srt_time_str_sec_to_time(endtime);						
						//subtitle dialog
						len = ekk_strlen(psub);
						p->tav_subtitle[index-1].ptext = ekk_malloc(len+1);
						ekk_memcpy(p->tav_subtitle[index-1].ptext, psub, len);
						*(p->tav_subtitle[index-1].ptext+len) = 0;
					}
				}
				
				
				
			}
			memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
			fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
			len = ekk_strlen(linetxt);
		}
		ekk_free(linetxt);
		linetxt = 0;
		p->cur_subtitle_index = 0;
		p->tav_subtitle_count = index;
		if (index > 0) {
			ret = 0;
		}		
		ekk_fclose(mfd);
        sort_subtitle_array(p);
	}
	return ret;
}
//decode smi file
int load_subtitle_smi_file(char* path,SUBTITLE_HANDLE hd)
{
	int ret = -1;
	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) {
		return ret;
	}	
	EKK_FILE_HANDLE mfd = ekk_fopen(path, "rb");
	if (mfd) {
		char* linetxt = ekk_malloc(MAX_SUBTITLE_LINE_NUM+1);
		fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);	
		char tmpbuf[2048]={0};
		char starttime[20] ={0};
		char endtime[20]= {0};
		char* psub = 0;
		int index = 0;
		int j =0;
		int len = ekk_strlen(linetxt);
		int mark = 0;
        int li = 0;
		//first find <BODY>
		while (len > 0) {
			//ekk_memcpy(tmpbuf, linetxt, len);
			psub = ekk_strstr(linetxt,"<BODY>");
			if (psub) {
				mark = 1;
				break;
			}
			memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
			fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
			len = ekk_strlen(linetxt);
            //转成大写
            for (li = 0; li < len; ++li)
            {
                if (linetxt[li] == '>')
                {
                    break;
                }
                linetxt[li] = ekk_toupper(linetxt[li]);
            }
		}
		//<BODY> get SubTitle
		while (mark ==1 && len >0) {
            //转成大写
            for (li = 0; li < len; ++li)
            {
                if (linetxt[li] == '>')
                {
                    break;
                }
                linetxt[li] = ekk_toupper(linetxt[li]);
            }
			psub = ekk_strstr(linetxt,"</BODY>");
			if (psub) {
				//end the subtitle body
				break;
			}
			//start time
			
			psub = ekk_strstr(linetxt, "<SYNC START=");
			if (psub) {
				j = 0;
				psub = psub +12;
				ekk_memset(starttime, 0, 20);
				while (*(psub+j) != '>' && j<20) {
					*(starttime+j) = *(psub+j);
					j++;
				}
				index++;
                j++;
				//get subtitle txt
				len = ekk_strlen(&psub[j]);
				p->tav_subtitle[index-1].ptext = ekk_malloc(len+1);
				ekk_memcpy(p->tav_subtitle[index-1].ptext, &psub[j], len);
				*(p->tav_subtitle[index-1].ptext+len) = 0;
				
    
				p->tav_subtitle[index-1].index = index;
				p->tav_subtitle[index-1].starttime = atoi(starttime);
				p->tav_subtitle[index-1].endtime = 0;
                if (index > 1 && p->tav_subtitle[index-2].endtime <= 0)
                {
                    p->tav_subtitle[index-2].endtime = p->tav_subtitle[index-1].starttime;
                    if (p->tav_subtitle[index-2].endtime - p->tav_subtitle[index-2].starttime > 10000)
                    {
                        p->tav_subtitle[index-2].endtime = p->tav_subtitle[index-2].starttime + 10000;
                    }
                }
			}		
			
			memset(linetxt,0,MAX_SUBTITLE_LINE_NUM+1);
			fgets(linetxt, MAX_SUBTITLE_LINE_NUM, mfd);
			len = ekk_strlen(linetxt);			
			
		}
		
		ekk_free(linetxt);
		linetxt = 0;
		p->cur_subtitle_index = 0;
		p->tav_subtitle_count = index;
		if (index > 0) {
			ret = 0;
		}
		ekk_fclose(mfd);
		sort_subtitle_array(p);
	}
	return ret;
}
// change file any format to utf-8
void file_any_format_to_utf8(char* pathin,char* pathout)
{
	char pout1[UTF8BLOCKS] = {0};
	char pout2[UTF8BLOCKS] = {0};
	char pout3[UTF8BLOCKS] = {0};
	char delaychar[3] = {0}; //lin jie utf8 fen ge
	//char pin[UTF8BLOCKS/2+3] = {0};
	int poutlen1,poutlen2,poutlen3;
	int inlen = UTF8BLOCKS/2;	
	int delaylen = 0;
	
	
	EKK_FILE_HANDLE mfd = ekk_fopen(pathin, "rb");
	if (mfd) {
		EKK_FILE_HANDLE mfd2 = ekk_fopen(pathout, "wb");
		if(mfd2)
		{
			
			long b = ekk_fseek(mfd,0,EKK_SEEK_END);
			long t = ekk_ftell(mfd);
			long c= ekk_fseek(mfd, 0, EKK_SEEK_SET);
			long mt = inlen;
			unsigned char* pin = ekk_malloc(t+1);
			unsigned char* pout= ekk_malloc(2*t);
			ekk_memset(pin, 0, t+1);
			ekk_memset(pout, 0, 2*t);
			ekk_fread(pin, 1, t, mfd);
			
			
			char* encode = NULL;
			//get cur file format
			
			if ((*pin == 0xFF && *(pin+1) == 0xFE) || (*pin == 0xFE && *(pin+1) == 0xFF)) {
				encode = "UTF-16";
				
			}
			else if((*pin == 0xef && *(pin+1) == 0xbb && *(pin+2) ==0xbf))
			{
				ekk_fwrite(pin+3, 1, t-3, mfd2);
				ekk_fclose(mfd2);
				ekk_fclose(mfd);
				return;				
			}
			else {
				///*
				int cp = sd_conjecture_code_page(pin);
				switch (cp) {
					case 0:
						encode = "ASCII";
						break;
					case 1:
						encode = "GBK";
						break;
					case 2:
						encode = "UTF-8";
						ekk_fwrite(pin, 1, t, mfd2);
						ekk_fclose(mfd2);
						ekk_fclose(mfd);
						return;
					case 3:
						encode = "BIG-5";
						break;
						
					default:
						break;
				}
				//*/ 
			}

			
			
		
			char* in = pin;
			char* out=pout;
			long inlen = t;
			long outlen = 2*t;
			iconv_t cd = iconv_open("UTF-8",encode);
			int hc = iconv(cd,&in,(size_t *)&inlen,&out,(size_t *)&outlen);
			ekk_fwrite(pout, 1, 2*t, mfd2);
			
			
			/*
			 while (mt <  t) {
			 poutlen1 = UTF8BLOCKS;
			 poutlen2 = UTF8BLOCKS;
			 poutlen3 = UTF8BLOCKS;
			 ekk_fread(pin+delaylen, 1, UTF8BLOCKS/2, mfd);
			 char *in = pin;
			 char *out1 =pout1;
			 char *out2 =pout2;
			 char *out3 =pout3;
			 
			 iconv(cd,in,UTF8BLOCKS/2 + delaylen-3,out1,&poutlen1);
			 iconv(cd,in,UTF8BLOCKS/2 + delaylen-2,out2,&poutlen2);
			 iconv(cd,in,UTF8BLOCKS/2 + delaylen-1,out3,&poutlen3);
			 if (poutlen1 < poutlen2) {					
			 ekk_memcpy(pin,pin+UTF8BLOCKS/2 + delaylen-2, 2);
			 ekk_fwrite(pout2, 1, poutlen2, mfd2);
			 delaylen = 2;
			 }
			 else if(poutlen2 < poutlen3)
			 {
			 ekk_memcpy(pin,pin+UTF8BLOCKS/2 + delaylen-1, 1);
			 ekk_fwrite(pout3, 1, poutlen3, mfd2);
			 delaylen = 1;
			 }
			 else {					
			 ekk_memcpy(pin,pin+UTF8BLOCKS/2 + delaylen-3, 3);
			 ekk_fwrite(pout1, 1, poutlen1, mfd2);
			 delaylen = 3;
			 }
			 
			 mt +=inlen;
			 }
			 if (mt-inlen < t) {
			 int nlen = t -mt +inlen;
			 poutlen1 = UTF8BLOCKS;
			 ekk_fread(pin, 1, nlen, mfd);
			 sd_any_format_to_utf8(pin,nlen,pout1,&poutlen1);
			 ekk_fwrite(pout1, 1, poutlen1, mfd2);
			 
			 }
			 //ekk_fwrite(rgb32_head, 1, 54, mfd2);
			 //ekk_fwrite(g_subtitle_rgb, 1, 400*400*4, mfd2);
			 
			 //*/ 
			iconv_close(cd);
			ekk_fclose(mfd2);
		}		
		ekk_fclose(mfd);
	}	
}
// get svv player cid
ekk_bool ekk_get_svvcid(char* path,char* cid)
{
	ekk_bool ret = ekk_true;
	
	EKK_FILE_HANDLE mfd = ekk_fopen(path, "rb");
	if (mfd) {
		long b = ekk_fseek(mfd,0,EKK_SEEK_END);
		long t = ekk_ftell(mfd);
		long c= ekk_fseek(mfd, 0, EKK_SEEK_SET);
		if (t < 1024*8) {
			ret =ekk_false;
		}
		else
		{
			long offset[4] = {0};
			offset[3] = t -8192;
			offset[2] = t/3;
			offset[1] = t/3 *2;
			offset[0] = 4096;
			char mbuf[4096] = {0};
			unsigned char tcid[4][16] = {0};
			int i = 0,readlen = 0;
			for (i = 0; i<4; i++) {
				c= ekk_fseek(mfd, offset[i], EKK_SEEK_SET);
				readlen = ekk_fread(mbuf, 1, 4096, mfd);
				if (readlen <= 0) {
					ret = ekk_false;
					break;
				}
				//calc md5 char
#if DOWNLOADOPEN				
				ctx_md5 context;
				md5_initialize(&context);
				md5_update(&context,(unsigned char*)mbuf, 4096);
				md5_finish(&context, tcid[i]);
#endif						
			}
			
			char hextmp[33]={0};
			ekk_memset(cid,0,32*4+1);
			for (i = 0; i<4; i++) {
#if DOWNLOADOPEN				
				str2hex(tcid[i], 16, hextmp, 32);
#endif					
				sprintf(cid,"%s%s",cid,hextmp);
			}
			
			///*
			int strlen = ekk_strlen(cid);
			for (i =0; i<strlen; i++) {
				if ('A' <= *(cid+i)) {
					*(cid+i) = 'F' - *(cid+i) + '0';
					
				}
				else if('6' <= *(cid+i))
				{
					*(cid+i) = ('0' +15 -*(cid+i)) + '0';
				}
				else {
					*(cid+i) = 'F' - (*(cid+i) - '0');
				}
				
			}
			//*/ 
		}
		ekk_fclose(mfd);
	}
	else {
		ret = ekk_false;
	}
	
	return ret;
}
//according the recv buf to get the subtitle file form net 
int ekk_get_url_data_from_recvbuf(char* recv,int len)
{
	if (len > 44) {
		int head = 44;
		int pos = head;
		int i =0,j=0;
		char slanguage[20]= {0};
		while (pos < len) {
			ekk_memset(g_match_ctx.subtitle_net_url[i].cid, 0, 41);
			ekk_memcpy(g_match_ctx.subtitle_net_url[i].cid,recv+pos,40);
			ekk_memset(g_match_ctx.subtitle_net_url[i].url, 0, 1024);
			ekk_sprintf(g_match_ctx.subtitle_net_url[i].url,"http://file.zimu.gougou.com/%c%c/%c%c/%s",g_match_ctx.subtitle_net_url[i].cid[0],\
						g_match_ctx.subtitle_net_url[i].cid[1],\
						g_match_ctx.subtitle_net_url[i].cid[38],\
						g_match_ctx.subtitle_net_url[i].cid[39],\
						g_match_ctx.subtitle_net_url[i].cid);
			pos = pos+ 40;
			if (*(recv+pos) == '&'&& *(recv+pos) == ':') {
				//is sub type &:
				pos +=2;
				
			}
			else if(*(recv+pos) == ':')
			{
				pos +=1;
			}
			else {
				break;
			}
			// language 
			j =0;
			while (*(recv+pos+j)!= ':' && (pos+j) < len) {
				j++;
			}
			ekk_memset(slanguage, 0, 20);
			ekk_memcpy(slanguage,recv+pos, j);
			g_match_ctx.subtitle_net_url[i].language = atoi(slanguage);
			
			pos +=j;
			//language next char :
			pos +=1;
			
			j =0;
			while ((*(recv+pos+j)!= '|' || *(recv+pos+j)!= 0) && (pos+j) < len) {
				char c = *(recv+pos+j);
				if (c == '|') {
					break;
				}
				j++;
			}			
			//format			
			ekk_memset(g_match_ctx.subtitle_net_url[i].format, 0, j+2);
			ekk_memcpy(g_match_ctx.subtitle_net_url[i].format,recv+pos, j);
			ekk_memset(g_match_ctx.subtitle_net_url[i].filename, 0, 256);
			ekk_sprintf(g_match_ctx.subtitle_net_url[i].filename,"%s.%s",g_match_net_type_str[g_match_ctx.subtitle_net_url[i].language + 1],g_match_ctx.subtitle_net_url[i].format);
			pos +=j;
			
			//format next char | or 0
			pos +=1;
			i++;
			
		}
		g_match_ctx.subtitle_net_url_count = i;
	}
}



#if EKK_ENABLE_NET_SUBTITLE
// http get sucs callback
static _int32 http_get_callback(ETM_HTTP_CALL_BACK* p_http_call_back_param)
{
	char* srecv = (char*)p_http_call_back_param->_user_data;
	int len = ekk_strlen(srecv);
	ekk_memset(s_http_recv, 0, 1024);
	ekk_memcpy(s_http_recv, srecv, len);
	http_request = 1;
}
// http post sucs callback
static _int32 http_post_callback(ETM_HTTP_CALL_BACK* p_http_call_back_param)
{
	_u32 http_id = 0;
	
	//http_post_sucs = 1;
    g_is_requesting_http = ekk_false;
    g_http_id = NONE_HTTP_ID;
	
	return 0;
}
//download net subtitle
void ekk_subtitle_download_file(char* url ,char* path, char* filename)
{
	ETM_HTTP_GET_FILE http_get_file ={0};
	_u32 http_id = 0;
    g_is_requesting_http = ekk_true;
	sd_strncpy(http_get_file._file_path, path,ekk_strlen(path));
	sd_strncpy(http_get_file._file_name, filename,ekk_strlen(filename));
	http_get_file._file_path_len = ekk_strlen(path);
	http_get_file._file_name_len = ekk_strlen(filename);
	http_get_file._url = url;
	http_get_file._url_len = ekk_strlen(url);
	http_get_file._cookie = NULL;
	
	http_get_file._cookie_len = 0;
	http_get_file._callback_fun = http_post_callback;
	http_get_file._user_data = NULL;
	http_get_file._accept_gzip = 0;
	http_get_file._timeout = 1000;
	_int32 ret = etm_http_get_file(&http_get_file,&http_id);
	if (ret == 0)
    {
        g_http_id = http_id;
	}
    else
    {
        g_is_requesting_http = ekk_false;
    }
}
#endif/*#if EKK_ENABLE_NET_SUBTITLE*/

int ekk_subtitle_get_match_path(SUBTITLE_MATCH_CONTEXT** pcontext)
{
	*pcontext = &g_match_ctx;
}
#if EKK_ENABLE_NET_SUBTITLE
// get the subtitle from net and match the best subtitle
ekk_bool ekk_get_subtitle_url_from_net(char* pmoiveurl,char* purl)
{
	//sever init 
	char* recvbuf = ekk_malloc(16384+1);
	//char* spid="001D092DB065G9UE";
	//cid
	char* scontent = ekk_malloc(1024);
	ekk_memset(scontent, 0, 1024);
	char p_cid[21]={0};
	char s_cid[40+128]={0};
	
	sd_calc_file_cid(pmoiveurl, p_cid);
	str2hex(p_cid, 20, s_cid, 40);
	
	//svvcid
	ekk_get_svvcid(pmoiveurl,(s_cid+40));
	ekk_memcpy(scontent+36, s_cid, 168);
	//ekk_memcpy(scontent+12,spid,16);
	ekk_memcpy(scontent+12,s_cid,16);
	/*
	 int* protocol_version = (int*)scontent;
	 *protocol_version = 3;
	 *(protocol_version+1) = 0;
	 *(protocol_version+2) = 0x0804;
	 //3*4 =12  12+16+4+4 +168 
	 *(protocol_version +7)= 2004;
	 *(protocol_version +8) = 0xA8;
	 //*/
	
	//*
	*scontent = 3;
	*(scontent+8) = 0x04;
	*(scontent+9) =0x08;
	*(scontent+28)= 0xD4;
	*(scontent+29)=0x07;
	*(scontent+32)=0xA8;	
	//*/
	/////////////////
	
	
	//char* sever = "http://zimu.gougou.com:18889";
	//char* sever = ekk_malloc(2048);
	//ekk_memset(sever, 0, 2048);
	//ekk_sprintf(sever, "%s%s","http://zimu.gougou.com:18889?iprotocol_version = 3&iseq_num =0&imagic=2052&sPeerid=&icmdid=2004&body_len=204&smoviecid=",scontent);
	int sendlen = 204;
	int recvlen = 1024;
    if (!g_url_has_been_canceled)
    {
        g_is_requesting_url = ekk_true;
        ekk_socket_deal(SUBTITLESEVERURL, SUBTITLESEVERMATCHPORT, scontent,&sendlen, recvbuf, &recvlen);
        g_is_requesting_url = ekk_false;
    }
    if (!g_url_has_been_canceled)
    {
        ekk_get_url_data_from_recvbuf(recvbuf,recvlen);
    }
	
	/*
	//download the subtitle file
	
	char subpath[512]={0};
	sprintf(subpath,"%s",io_get_default_path());//,g_subtitle_net_url[0].cid);
	
	
	ekk_subtitle_download_file(g_subtitle_net_url[0].url,subpath,g_subtitle_net_url[0].cid);
	
	ekk_memset(subpath, 0, 512);
	
	sprintf(subpath,"%s%s",io_get_default_path(),g_subtitle_net_url[0].cid);
	ekk_subtitle_load_from_out_file(subpath,g_subtitle_net_url[0].format);
	//*/
	//post test
	/*
	 ETM_HTTP_POST http_post = {0};
	 _u32 http_id = 0;
	 http_post._url = sever;
	 http_post._url_len = ekk_strlen(sever);
	 http_request = 0;
	 //post content
	 ///
	 char* recvbuf = ekk_malloc(16384+1);
	 ekk_memset(recvbuf,0,16384+1);
	 http_post._content_len = 204;
	 http_post._send_data = scontent;
	 http_post._send_data_len = 204;
	 http_post._recv_buffer = recvbuf;
	 http_post._recv_buffer_size = 16384+1;
	 http_post._callback_fun = http_post_callback;
	 http_post._user_data = sever;
	 http_post._timeout = 20;
	 _int32 ret = etm_http_post(&http_post,&http_id);
	 if(0 != ret)
	 {
	 return ekk_false;
	 }
	 //*/
	
	/*
	 //get test
	 _u32 http_id = 0;
	 char* recvstr = ekk_malloc(1*1024+1);
	 ekk_memset(recvstr, 0, 1*1024+1);
	 //char* pmoiveurl = "http://zimu.gougou.com:18889";
	 ETM_HTTP_GET http_get = {0};
	 http_get._url = sever;
	 http_get._url_len = ekk_strlen(sever);
	 http_get._recv_buffer = recvstr;
	 http_get._recv_buffer_size = 1*1024+1;
	 http_get._callback_fun = http_get_callback;
	 http_get._user_data = recvstr;
	 http_get._timeout = 20;	
	 _int32 ret = etm_http_get(&http_get, &http_id);
	 if(0 != ret)
	 {
	 
	 }
	 //*/
	ekk_free(scontent);
	ekk_free(recvbuf);
	scontent = NULL;
	recvbuf = NULL;
	return ekk_true;
}
#endif

/*
//out subtitle seek pos ,updata the subtitle cur info
int ekk_subtitle_seek_at_time(long long timestamp)
{
	int ret = 0;
	int i = 0;
	int status = 0;
	if (g_tav_subtitle_count > 1)
	{
		if (timestamp <1000*g_tav_subtitle[0].starttime) {
			cur_subtitle_index = 0;
		}
		else if(timestamp >1000*g_tav_subtitle[g_tav_subtitle_count -1].endtime)
		{
			cur_subtitle_index = g_tav_subtitle_count;
		}
		else if(timestamp < 1000*g_tav_subtitle[g_tav_subtitle_count -1].starttime && timestamp >1000*g_tav_subtitle[g_tav_subtitle_count -2].endtime)
		{
			cur_subtitle_index = g_tav_subtitle_count-2;
		}	
		else {
			for (i =0; i<g_tav_subtitle_count-1; i++) {
				if (timestamp >= 1000*g_tav_subtitle[i].starttime && timestamp <= 1000*g_tav_subtitle[i].endtime)
				{
					cur_subtitle_index = i;
					break;
				}
				else if(timestamp > 1000*g_tav_subtitle[i].endtime && timestamp <1000*g_tav_subtitle[i+1].starttime){
					cur_subtitle_index = i;
					break;
				}

				
			}			
		}

	}	
	return ret;
}
//*/ 
// ret  2: curtime have no  subtitle  1: curtime have subtitle  0: no subtitle file or have end the subtitle or have  3: have subtitle but at the time section have get once
EKK_SUBTITLE_MARK ekk_subtitle_get_buffer_at_time(SUBTITLE_HANDLE hd,long long timestamp,unsigned char** buf)
{
	int ret = _SUBTITLE_NO_CONTENT;
	

	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) {
		return ret;
	}
	int i =p->cur_subtitle_index;
	int j = 0;
	if ( p->tav_subtitle_count && i<=  p->tav_subtitle_count-1) {
		
		long long stime = 1000*p->tav_subtitle[i].starttime;
		
		if (timestamp > stime) {
			if (timestamp >= 1000*p->tav_subtitle[p->tav_subtitle_count-1].starttime) {
				
			
				if(timestamp > 1000*p->tav_subtitle[p->tav_subtitle_count-1].endtime)			
				{
					p->cur_subtitle_index = p->tav_subtitle_count-1;
					p->cur_subtitle_get_num = 0;
					p->cur_no_content_num++;
					if (p->cur_no_content_num > 1) {
						ret = _SUBTITLE_NO_CONTENT;
						return ret;
					}					
					char* p_bitmap = NULL;
					dec_subtitle_get_empty_bitmap(&p_bitmap,p->p_sub);
					*buf = p_bitmap;					
					ret = _SUBTITLE_NO_CONTENT_ONCE;
					
				}
				else //(timestamp >= 1000*g_tav_subtitle[g_tav_subtitle_count-1].starttime && timestamp <= 1000*g_tav_subtitle[g_tav_subtitle_count-1].endtime)
				{
					p->cur_subtitle_index = p->tav_subtitle_count-1;
					p->cur_subtitle_get_num++;
					p->cur_no_content_num = 0;
					if (p->cur_subtitle_get_num > 1) {
						ret = _SUBTITLE_MORE_HAVED;
						return ret;
					}
					//memset(g_subtitle_rgba,0,g_subtitle_rgba_size);
					char* p_bitmap = NULL;
					dec_subtitle_to_bitmap(&p_bitmap, p->tav_subtitle[p->cur_subtitle_index].ptext,p->p_sub);
					*buf = p_bitmap;
					ret = _SUBTITLE_HAVE_ONCE;			
				}	
			
		}
		else {

			for (j = p->cur_subtitle_index; j <p->tav_subtitle_count-1; j++) {
				if (timestamp >= 1000*p->tav_subtitle[j].starttime && timestamp <= 1000*p->tav_subtitle[j].endtime) {
					p->cur_subtitle_index = j;
					p->cur_subtitle_get_num++;
					p->cur_no_content_num = 0;
					if (p->cur_subtitle_get_num > 1) {
						ret = _SUBTITLE_MORE_HAVED;
						break;
					}
					//memset(g_subtitle_rgba,0,g_subtitle_rgba_size);
					//long long b_ts,e_ts;
					//b_ts = ekk_get_time_in_microsecond();
					char* p_bitmap = NULL;
					dec_subtitle_to_bitmap(&p_bitmap, p->tav_subtitle[j].ptext,p->p_sub);
					
					//e_ts = ekk_get_time_in_microsecond();
					//printf("\n dec_subtitle_to_bitmap **********time = %lld \n",e_ts - b_ts);
					*buf = p_bitmap;
					ret = _SUBTITLE_HAVE_ONCE;
					break;
				}
				else if(timestamp >= 1000*p->tav_subtitle[j].endtime && timestamp <= 1000*p->tav_subtitle[j+1].starttime)
				{

					p->cur_subtitle_index = j;
					p->cur_subtitle_get_num = 0;
					p->cur_no_content_num++;
					if (p->cur_no_content_num > 1) {
						ret = _SUBTITLE_NO_CONTENT;
						return ret;
					}					
					char* p_bitmap = NULL;
					dec_subtitle_get_empty_bitmap(&p_bitmap,p->p_sub);
					*buf = p_bitmap;					
					ret = _SUBTITLE_NO_CONTENT_ONCE;
					break;
				}
				p->cur_subtitle_get_num = 0;
			}
			}	
		}
		else if(timestamp < stime)
		{
			if(timestamp<1000*p->tav_subtitle[0].starttime)			
			{
				p->cur_subtitle_index = 0;
				p->cur_subtitle_get_num = 0;
				p->cur_no_content_num++;
				if (p->cur_no_content_num > 1) {
					ret = _SUBTITLE_NO_CONTENT;
					return ret;
				}					
				char* p_bitmap = NULL;
				dec_subtitle_get_empty_bitmap(&p_bitmap,p->p_sub);
				*buf = p_bitmap;					
				ret = _SUBTITLE_NO_CONTENT_ONCE;
			}
			else {
				for (j = 0; j< i; j++) {
					if (timestamp >= 1000*p->tav_subtitle[j].starttime && timestamp <= 1000*p->tav_subtitle[j].endtime) {
						p->cur_subtitle_index = j;
						p->cur_subtitle_get_num++;
						p->cur_no_content_num = 0;
						if (p->cur_subtitle_get_num > 1) {
							ret = _SUBTITLE_MORE_HAVED;
							break;
						}
						char* p_bitmap = NULL;
						dec_subtitle_to_bitmap(&p_bitmap, p->tav_subtitle[p->cur_subtitle_index].ptext,p->p_sub);
						*buf = p_bitmap;
						ret = _SUBTITLE_HAVE_ONCE;
						break;
					}
					else if(timestamp > 1000*p->tav_subtitle[j].endtime && timestamp < 1000*p->tav_subtitle[j+1].starttime)
					{
						p->cur_subtitle_index = j;
						p->cur_subtitle_get_num = 0;
						p->cur_no_content_num++;
						if (p->cur_no_content_num > 1) {
							ret = _SUBTITLE_NO_CONTENT;
							return ret;
						}					
						char* p_bitmap = NULL;
						dec_subtitle_get_empty_bitmap(&p_bitmap,p->p_sub);
						*buf = p_bitmap;					
						ret = _SUBTITLE_NO_CONTENT_ONCE;
						break;
					}
					p->cur_subtitle_get_num = 0;
				}
			}
		}
		else
		{
			p->cur_subtitle_get_num++;
			p->cur_no_content_num = 0;
			if (p->cur_subtitle_get_num > 1) {
				ret = _SUBTITLE_MORE_HAVED;
				return ret;
			}			
			char* p_bitmap = NULL;
			dec_subtitle_to_bitmap(&p_bitmap, p->tav_subtitle[i].ptext,p->p_sub);
			*buf = p_bitmap;
			ret = _SUBTITLE_HAVE_ONCE;				
		}	
		
	}	
	/*
	if (g_tav_subtitle_count && i< g_tav_subtitle_count-1) {
		 if(timestamp <= 1000*g_tav_subtitle[i].starttime)
		 {
			 cur_subtitle_get_num = 0;
			 cur_subtitle_index = i-1 >=0? i-1:0;
			 ret = _SUBTITLE_NO_CONTENT;
		 }
		 else if(timestamp >= 1000*g_tav_subtitle[i].endtime && timestamp <= 1000*g_tav_subtitle[i+1].starttime)
		 {
			 cur_subtitle_get_num = 0;
			 cur_subtitle_index = i;
			 ret = _SUBTITLE_NO_CONTENT;
		 }
		 else if (timestamp >= 1000*g_tav_subtitle[i].starttime && timestamp <= 1000*g_tav_subtitle[i].endtime) {
			 cur_subtitle_get_num++;			 
			 cur_subtitle_index = i;
			 if (cur_subtitle_get_num > 1) {
				 ret = _SUBTITLE_MORE_HAVED;
				 return ret;
			 }
			 memset(g_subtitle_rgba,0,g_subtitle_rgba_size);
			 dec_subtitle_to_bitmap(g_subtitle_rgba, g_tav_subtitle[i].ptext);
			 *buf = g_subtitle_rgba;
			 ret = _SUBTITLE_HAVE_ONCE;
			 
		 }
		 else if (timestamp > 1000*g_tav_subtitle[i+1].starttime && timestamp <= 1000*g_tav_subtitle[i+1].endtime) {
			 cur_subtitle_get_num++;
			 cur_subtitle_index = i+1;
			 if (cur_subtitle_get_num > 1) {
				 ret = _SUBTITLE_MORE_HAVED;
				 return ret;
			 }			 
			 memset(g_subtitle_rgba,0,g_subtitle_rgba_size);
			 dec_subtitle_to_bitmap(g_subtitle_rgba, g_tav_subtitle[i+1].ptext);
			 *buf = g_subtitle_rgba;
			 ret = _SUBTITLE_HAVE_ONCE;
		 }	 
	 }
	//*/
	return ret;
}


// ret  2: curtime have no  subtitle  1: curtime have subtitle  0: no subtitle file or have end the subtitle or have  3: have subtitle but at the time section have get once
EKK_SUBTITLE_MARK ekk_subtitle_get_text_at_time(SUBTITLE_HANDLE hd,long long timestamp,unsigned char** buf)
{
	//int ret = _SUBTITLE_NO_CONTENT;	
	//modify.by.wangssheng
	int ret = _SUBTITLE_NO_CONTENT_ONCE;
	
	SUBTITLE_CONTEXT* p = (SUBTITLE_CONTEXT*)hd;
	if (!p) {
		return ret;
	}
	int i =p->cur_subtitle_index;
	int j = 0;
	if ( p->tav_subtitle_count && i<=  p->tav_subtitle_count-1) {
		
		long long stime = 1000*p->tav_subtitle[i].starttime;
		
		if (timestamp > stime) {
			if (timestamp >= 1000*p->tav_subtitle[p->tav_subtitle_count-1].starttime) {
				
			
				if(timestamp > 1000*p->tav_subtitle[p->tav_subtitle_count-1].endtime)			
				{
					p->cur_subtitle_index = p->tav_subtitle_count-1;
					p->cur_subtitle_get_num = 0;
					p->cur_no_content_num++;
					if (p->cur_no_content_num > 1) {
						ret = _SUBTITLE_NO_CONTENT;
						return ret;
					}		
					*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;					
					ret = _SUBTITLE_NO_CONTENT_ONCE;
					
				}
				else //(timestamp >= 1000*g_tav_subtitle[g_tav_subtitle_count-1].starttime && timestamp <= 1000*g_tav_subtitle[g_tav_subtitle_count-1].endtime)
				{
					p->cur_subtitle_index = p->tav_subtitle_count-1;
					p->cur_subtitle_get_num++;
					p->cur_no_content_num = 0;
					if (p->cur_subtitle_get_num > 1) {
						ret = _SUBTITLE_MORE_HAVED;
						return ret;
					}
					*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;
					ret = _SUBTITLE_HAVE_ONCE;			
				}	
			
		}
		else {

			for (j = p->cur_subtitle_index; j <p->tav_subtitle_count-1; j++) {
				if (timestamp >= 1000*p->tav_subtitle[j].starttime && timestamp <= 1000*p->tav_subtitle[j].endtime) {
					p->cur_subtitle_index = j;
					p->cur_subtitle_get_num++;
					p->cur_no_content_num = 0;
					if (p->cur_subtitle_get_num > 1) {
						ret = _SUBTITLE_MORE_HAVED;
						break;
					}
					*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;
					ret = _SUBTITLE_HAVE_ONCE;
					break;
				}
				else if(timestamp >= 1000*p->tav_subtitle[j].endtime && timestamp <= 1000*p->tav_subtitle[j+1].starttime)
				{

					p->cur_subtitle_index = j;
					p->cur_subtitle_get_num = 0;
					p->cur_no_content_num++;
					if (p->cur_no_content_num > 1) {
						ret = _SUBTITLE_NO_CONTENT;
						return ret;
					}					
					*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;				
					ret = _SUBTITLE_NO_CONTENT_ONCE;
					break;
				}
				p->cur_subtitle_get_num = 0;
			}
			}	
		}
		else if(timestamp < stime)
		{
			if(timestamp<1000*p->tav_subtitle[0].starttime)			
			{
				p->cur_subtitle_index = 0;
				p->cur_subtitle_get_num = 0;
				p->cur_no_content_num++;
				if (p->cur_no_content_num > 1) {
					ret = _SUBTITLE_NO_CONTENT;
					return ret;
				}					
				*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;				
				ret = _SUBTITLE_NO_CONTENT_ONCE;
			}
			else {
				for (j = 0; j< i; j++) {
					if (timestamp >= 1000*p->tav_subtitle[j].starttime && timestamp <= 1000*p->tav_subtitle[j].endtime) {
						p->cur_subtitle_index = j;
						p->cur_subtitle_get_num++;
						p->cur_no_content_num = 0;
						if (p->cur_subtitle_get_num > 1) {
							ret = _SUBTITLE_MORE_HAVED;
							break;
						}
						*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;
						ret = _SUBTITLE_HAVE_ONCE;
						break;
					}
					else if(timestamp > 1000*p->tav_subtitle[j].endtime && timestamp < 1000*p->tav_subtitle[j+1].starttime)
					{
						p->cur_subtitle_index = j;
						p->cur_subtitle_get_num = 0;
						p->cur_no_content_num++;
						if (p->cur_no_content_num > 1) {
							ret = _SUBTITLE_NO_CONTENT;
							return ret;
						}					
						*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;			
						ret = _SUBTITLE_NO_CONTENT_ONCE;
						break;
					}
					p->cur_subtitle_get_num = 0;
				}
			}
		}
		else
		{
			p->cur_subtitle_get_num++;
			p->cur_no_content_num = 0;
			if (p->cur_subtitle_get_num > 1) {
				ret = _SUBTITLE_MORE_HAVED;
				return ret;
			}			
			*buf = p->tav_subtitle[p->cur_subtitle_index].ptext;
			ret = _SUBTITLE_HAVE_ONCE;				
		}	
		
	}	

	return ret;
}

#if EKK_ENABLE_NET_SUBTITLE
//local film file upload
int ekk_subtitle_upload_file(char* filmpath,char* subtitlepath)
{
	int ret =-1;
	//query can upload
	
	char* recvbuf = ekk_malloc(16384+1);
	char* scontent = ekk_malloc(1024);
	ekk_memset(scontent, 0, 1024);	
	

	//*
	*scontent = 1;
	*(scontent+8) = 0x04;
	*(scontent+9) =0x08;
	*(scontent+28)= 0xD1;
	*(scontent+29)=0x07;
	*(scontent+32)=0x28;	
	//*/	
	//cid
	char p_cid[21]={0};
	char s_cid[40]={0};
	
	sd_calc_file_cid(subtitlepath, p_cid);
	str2hex(p_cid, 20, s_cid, 40);
	ekk_memcpy(scontent+12,s_cid,16);
	ekk_memcpy(scontent+36, s_cid, 40);
	//make a query can upload subtitle file packet packet size 36+40
	
	int sendlen = 76;
	int recvlen = 1024;
	ekk_socket_deal(SUBTITLESEVERURL, SUBTITLESEVERUPLOADPORT, scontent,&sendlen, recvbuf, &recvlen);
	if (recvlen >= 40) {
		if (*(recvbuf+36) == 0 && *(recvbuf+37) == 0 && *(recvbuf+38) == 0 && *(recvbuf+39) == 0) {
		//recv packet ex this subtitle can upload	
		//make a send file packet
		*(scontent+28)= 0xD2;
		EKK_FILE_HANDLE mfd = ekk_fopen(subtitlepath, "rb");
		if (mfd) {
				long b = ekk_fseek(mfd,0,SEEK_END);
				long t = ekk_ftell(mfd);
				long c= ekk_fseek(mfd, 0, SEEK_SET);
				int mt = t+44;
				*(scontent+34)=(char)(mt/(64*1024));
				mt = mt%(64*1024);
				*(scontent+33)=(char)(mt/256);
				mt = mt % 256;
				*(scontent+32)=(char)mt;
				mt = t ;
				*(scontent+78)=(char)(mt/(64*1024));
				mt = mt%(64*1024);
				*(scontent+77)=(char)(mt/256);
				mt = mt % 256;
				*(scontent+76)=(char)mt;
			
				char* ssendbuf = ekk_malloc(t+80);
				ekk_memcpy(ssendbuf,scontent,80);
				ekk_fread(ssendbuf+80,1,t,mfd);
				sendlen = t+ 80;
				ekk_socket_deal(SUBTITLESEVERURL, SUBTITLESEVERUPLOADPORT, ssendbuf,&sendlen, recvbuf, &recvlen);
				ekk_fclose(mfd);
		}	
			
		}
	}
	
	ekk_free(scontent);
	ekk_free(recvbuf);
	scontent = NULL;
	recvbuf = NULL;	
	return ret;
}
#endif/*#if EKK_ENABLE_NET_SUBTITLE*/

//according the format to decode subtitle file
int ekk_subtitle_load_from_out_file(SUBTITLE_HANDLE hd,char* path,char* format)
{
	//any format to utf-8
	char pathout[512]= {0};
	int len = ekk_strlen(path);
	char* psub = path;
	char* filename = psub;
	while (psub != NULL) {
		psub++;
		filename = psub;
		psub = ekk_strchr(psub, '/');
	}	
	
	//Library/Caches
	sprintf(pathout,"%stmp/%s",ekk_getcwd(),filename);
	file_any_format_to_utf8(path,pathout);
	
	
	//
	if(NULL == format)
	{
		char tmpformat[5] ={0};
		ekk_memcpy(tmpformat,path+len-4,4);
		if(tmpformat[0] == '.')
		{
			format = tmpformat+1;
		}
		else
		{
			format = "srt";
		}
	}	
	int ret = -1;
	if (format == NULL) {
		format = "srt";
	}
	if (ekk_strcmp(format,"srt") == 0 || ekk_strcmp(format,"SRT") == 0) {
		ret = load_subtitle_srt_file(pathout,hd);
	}
	else if(ekk_strcmp(format,"ass") == 0 || ekk_strcmp(format,"ssa") == 0 \
			|| ekk_strcmp(format,"ASS") == 0 || ekk_strcmp(format,"SSA") == 0){
		ret = load_subtitle_ssa_file(pathout,hd);

	}
	else if(ekk_strcmp(format,"smi") == 0 || ekk_strcmp(format,"SMI") == 0)
	{
		ret = load_subtitle_smi_file(pathout,hd);
	}	
	else if(ekk_strcmp(format,"psb") == 0 || ekk_strcmp(format,"PSB") == 0)
	{
		ret = load_subtitle_psb_file(pathout,hd);
	}
	
    if (ret != -1)
    {
        subtitle_split_multilines(hd);
    }
	
	return ret;
}

#endif /*#if EKK_ENABLE_SUBTITLE*/
